import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/tools/cn_app_webView.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../net/cn_net_api.dart';
import 'package:launch_review/launch_review.dart';
import 'package:package_info_plus/package_info_plus.dart';
// import '../../ui/base_web.dart';

class CnAboutPage extends StatefulWidget {
  const CnAboutPage({super.key});

  @override
  State<CnAboutPage> createState() => _AboutState();
}

class _AboutState extends State<CnAboutPage> {
  String version = '';

  @override
  void initState() {
    super.initState();
    getVersion();
  }

  getVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    print(packageInfo.version);
    setState(() {
      version = packageInfo.version;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: const Text(
          'About',
          style: TextStyle(fontSize: 18, color: Colors.black),
          textAlign: TextAlign.center,
        ),
        centerTitle: true, //标题居中
        leading: IconButton(
            color: Colors.black,
            icon: const Icon(Icons.arrow_back_ios),
            onPressed: () => Navigator.pop(context)),
      ),
      body: Container(
        child: SafeArea(
          child: Column(
            children: [
              Container(
                width: double.infinity,
                height: 200,
                child: Column(
                  children: [
                    Container(
                      padding: const EdgeInsets.only(top: 32, bottom: 10),
                      width: 138,
                      height: 138,
                      child: Image.asset(
                        'images/app_logo.png',
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(bottom: 5),
                      child: const Text(
                        'CyberNex',
                        style:
                            TextStyle(color: Color(0xFF3D3D3D), fontSize: 18),
                      ),
                    ),
                    Text(
                      'V${version}',
                      style: TextStyle(color: Color(0xFF636C78), fontSize: 14),
                    )
                  ],
                ),
              ),
              _myListTile(context, title: 'Terms & Conditions'),
              _myListTile(context, title: 'Privcay Policy'),
            ],
          ),
        ),
      ),
    );
  }
}

Widget _myListTile(BuildContext context, {required String title}) {
  return InkWell(
    onTap: () async {
      switch (title) {
        case 'Terms & Conditions':
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => const CnBaseWebView(
                      url: CnApiNet.kUserProtocolUrl,
                      title: 'Terms & Conditions',
                    )),
          );
          break;
        case 'Privcay Policy':
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => const CnBaseWebView(
                      url: CnApiNet.kPrivacyPolicyUrl,
                      title: 'Privcay Policy',
                    )),
          );
          break;
        case 'Rate Us':
          print('Rate Us');
          PackageInfo packageInfo = await PackageInfo.fromPlatform();

          /// The package name. `bundleIdentifier` on iOS, `getPackageName` on Android.
          var appIdentifier = packageInfo.packageName;
          LaunchReview.launch(
              androidAppId: appIdentifier, iOSAppId: appIdentifier);
          break;
        default:
      }
    },
    child: Container(
      decoration: const BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: ListTile(
        title: Text(title),
        trailing: Image.asset(
          'images/ic_arrow_black.png',
          width: 20,
          height: 15,
        ),
      ),
    ),
  );
}
